﻿using DanceIntegration.Models;
using DanceIntegration.Models.db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DanceIntegration.Controllers
{
    public class SectionController : Controller
    {

        public ActionResult Index()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            
                SectionListViewModel model = new SectionListViewModel()
                {
                    Sections = context.Sections.Select(x => new SectionViewModel(x))
                };
                return View(model);
            
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(int? parentId, string name)
        {
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                var newSect = new Section()
                {
                    PrevSectionID = parentId,
                    Name = name
                };
                context.Sections.Add(newSect);
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult Move(int nodeId, int? newParentId)
        {
            if (nodeId == newParentId)
            {
                return RedirectToAction("Index");
            }
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                if (newParentId.HasValue && ContainsChilds(context, nodeId, newParentId.Value))
                {
                    return RedirectToAction("Index");
                }
                var node = context.Sections.Where(x => x.ID == nodeId).Single();
                node.PrevSectionID = newParentId;
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        private bool ContainsChilds(ApplicationDbContext context, int parentId, int id)
        {
            bool result = false;
            var inner = context.Sections.Where(x => x.PrevSectionID == parentId ).ToArray();
            foreach (var node in inner)
            {
                if (node.ID == id && node.PrevSectionID == parentId)
                {
                    return true;
                }
                result = ContainsChilds(context, node.ID, id);
            }
            return result;
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                DeleteNodes(context, id);
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        private void DeleteNodes(ApplicationDbContext context, int id)
        {
            var inner = context.Sections.Where(x => x.PrevSectionID == id).ToArray();
            foreach (var node in inner)
            {
                DeleteNodes(context, node.ID);
            }
            var deleted = context.Sections.Where(x => x.ID == id).Single();
            
        }


    }
}